package com.deco.back.tags;

import com.deco.domain.vo.sys.CmsRoleVo;
import com.deco.domain.vo.sys.CmsUserVo;
import com.deco.service.sys.CmsMenuService;
import com.deco.service.sys.CmsRoleService;
import com.free.ext.web.spring.context.ServletContext;
import freemarker.core.Environment;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
@Component
public class ProfileDirective implements TemplateDirectiveModel {

    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/

    @Autowired
    private CmsRoleService roleService;


    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        String domainName = map.get("domainName").toString();
        CmsUserVo userVo = (CmsUserVo)SecurityUtils.getSubject().getPrincipal();
        List<CmsRoleVo> roles = roleService.getCmsRoleInfo(userVo.getRoles());
        ArrayList<String> roleNms = new ArrayList<>();
        roles.forEach(x->{
            roleNms.add(x.getName());
        });
        String html = genHtml(userVo, StringUtils.join(roleNms,","), domainName);
        environment.getOut().write(html);
        if(templateDirectiveBody != null){
            templateDirectiveBody.render(environment.getOut());
        }

    }


    public static BeansWrapper getBeansWrapper(){
        BeansWrapper beansWrapper =
                new BeansWrapperBuilder(Configuration.VERSION_2_3_27).build();
        return beansWrapper;
    }

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
    private String genHtml(CmsUserVo userInfo, String roles, String domainNm){
        if(userInfo == null){
            return StringUtils.EMPTY;
        }
         String html =
                 " <li class=\"nav-header\">" +
                " 	<div class=\"dropdown profile-element\">" +
                " 		<img alt=\"image\" class=\"rounded-circle\" src=\"/img/profile_small.jpg\">" +
                " 		<a data-toggle=\"dropdown\" class=\"dropdown-toggle\" href=\"#\">" +
                " 			<span class=\"block m-t-xs font-bold\">"+ userInfo.getUserName() + "</span>" +
                " 			<span class=\"text-muted text-xs block\">系统管理员<b class=\"caret\"></b></span>" +
                " 		</a>" +
                " 		<ul class=\"dropdown-menu animated fadeInRight m-t-xs\">" +
                " 			<li><a class=\"dropdown-item\" href=\"profile.html\">密码设置</a></li>" +
                " 			<li class=\"dropdown-divider\"></li>" +
                " 			<li><a class=\"dropdown-item\" href=\"/security/logout.html\">退出登录</a></li>" +
                " 		</ul>" +
                " 	</div>" +
                " 	<div class=\"logo-element\">" + domainNm + "</div>" +
                " </li>";
         return html;
    }

}
